package com.zhongyouyun.wx.wxapplet.cqyc.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lowagie.text.pdf.PRAcroForm;
import com.zhongyouyun.cqyc.model.LawModel;
import com.zhongyouyun.cqyc.model.LawTypeModel;
import com.zhongyouyun.cqyc.model.NoticeModel;
import com.zhongyouyun.cqyc.pojo.LawPojo;
import com.zhongyouyun.cqyc.service.LawService;
import com.zhongyouyun.cqyc.service.LawTypeService;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("wx/wxapplet/cqyc/law")
@Api(value = "烟草城区-微信小程序法律法规讲堂操作", tags = {"烟草城区-微信小程序法律法规讲堂操作"})
public class WxAppletLawController {
    @Autowired
    private LawTypeService lawTypeService;
    @Autowired
    private LawService lawService;

    @GetMapping("/findTPList")
    @ResponseBody
    @ApiOperation(value = "法律法规获取带图片的前5条数据", notes = "法律法规获取带图片的前5条数据")
    public Result findTPList() {
        QueryWrapper<LawModel> queryWrapper = new QueryWrapper();
        queryWrapper.eq("state","1");
        queryWrapper.ne("video_address","");
        queryWrapper.isNotNull("video_address");
        queryWrapper.orderByDesc("createBy");
        queryWrapper.last("LIMIT 5");
        List<LawModel> noticeModels = lawService.selectList(queryWrapper);
        List<Map> maps = new ArrayList<>();
        for (LawModel lawModel:noticeModels) {
            String substring = delHTMLTag(lawModel.getContext());
            if(substring.length() > 60){
                substring = substring.substring(0,59)+"...";
            }
            lawModel.setContext(substring);
            LawTypeModel lawTypeModel = lawTypeService.getById(lawModel.getLawId());
            Map map = new HashMap();
            map.put("lawModel",lawModel);
            map.put("lawTypeModel",lawTypeModel);
            maps.add(map);
        }
        return Result.builder().message("成功").status(0).data(maps).build();
    }

    @GetMapping("/findList")
    @ResponseBody
    @ApiOperation(value = "法律法规讲堂内容类型列表", notes = "法律法规讲堂内容类型列表")
    public Result<List<LawTypeModel>> findList() {
        QueryWrapper<LawTypeModel> queryWrapper = new QueryWrapper();
        queryWrapper.eq("state","1");
        List<LawTypeModel> list = lawTypeService.selectList(queryWrapper);
        if (null != list && list.size() > 0) {
            return Result.<List<LawTypeModel>>builder().message("成功").status(0).data(list).build();
        } else {
            return Result.<List<LawTypeModel>>builder().status(0).build();
        }
    }

    @GetMapping("/findPage")
    @ResponseBody
    @ApiOperation(value = "法律法规讲堂内容分页列表", notes = "法律法规讲堂内容分页列表")
    public IPage<LawPojo> findPage(@ApiParam(name = "当前第几页", value = "currentPage", required = true) @RequestParam(value = "currentPage") int currentPage,
                                   @ApiParam(name = "一页查几个", value = "pageSize", required = true) @RequestParam(value = "pageSize") int pageSize, @ApiParam(name = "类型ID", value = "lawId", required = false) @RequestParam(value = "lawId",required = false) String lawId) {
        QueryWrapper<LawPojo> queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(lawId)) {
            queryWrapper.eq("law_id", lawId);
        }
        queryWrapper.eq("state","1");
        Page page = new Page(currentPage, pageSize);
        IPage<LawPojo> lawPojoIPage = lawService.selectPojoPage(page, queryWrapper);
        List<LawPojo> records = lawPojoIPage.getRecords();
        for (LawPojo noticePojo:records) {
            String substring = delHTMLTag(noticePojo.getContext());
            if(substring.length() > 60){
                substring = substring.substring(0,59)+"...";
            }
            noticePojo.setContext(substring);
        }
        IPage<LawPojo> ipage = new Page<>();
        ipage.setRecords(records);
        return ipage;
    }

    @GetMapping("/getDetails")
    @ResponseBody
    @ApiOperation(value = "获取法律法规信息", notes = "获取法律法规信息")
    public Result getDetails(@ApiParam(name = "口袋书ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        LawModel lawModel = lawService.getById(id);
        if (ObjectUtil.isNotEmpty(lawModel)) {
            return Result.builder().message("成功").status(0).data(lawModel).build();
        } else {
            return Result.builder().message("获取失败").success(false).status(1).build();
        }
    }

    public static String delHTMLTag(String htmlStr){
        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
        Matcher m_script=p_script.matcher(htmlStr);
        htmlStr=m_script.replaceAll(""); //过滤script标签

        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
        Matcher m_style=p_style.matcher(htmlStr);
        htmlStr=m_style.replaceAll(""); //过滤style标签

        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
        Matcher m_html=p_html.matcher(htmlStr);
        htmlStr=m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }
}
